﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Pg.BioMedics.SDR.Data.Caching;

namespace Pg.BioMedics.SDR.Services.Impl
{
    /// <summary>
    /// Provides logic for cache managment
    /// </summary>
    internal class CacheManagementService : ICacheManagementService
    {
        #region ICacheManagementService Members

        /// <summary>
        /// Fills all data caches with data. This operation blocks current thread 
        /// execution.
        /// </summary>
        public void Fill()
        {
            try
            {
                SDRCachedDataContext.FillCaches();
            }
            catch (Exception ex)
            {
                throw new ServiceException("Couldnot fill caches", ex);
            }
        }

        /// <summary>
        /// Clears all datacaches and performs GC. This operation blocks 
        /// current thread for the execution time.
        /// </summary>
        public void Clear()
        {
            try
            {
                SDRCachedDataContext.ClearCaches();
            }
            catch (Exception ex)
            {
                throw new ServiceException("Couldnot clear caches", ex);
            }
        }

        /// <summary>
        /// Checks if caches has been initialized 
        /// </summary>
        /// <returns>true - if initialized, false otherwise</returns>
        public bool IsInitialized()
        {
            try
            {
                return (SDRCachedDataContext.Status == CachedDataContextStatus.Initialized);
            }
            catch (Exception ex)
            {
                throw new ServiceException("Couldnot get cached data context status", ex);
            }
        }

        #endregion
    }
}
